
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const TemplatedPathPlugin = require('./plugins/my-plugin.js')

module.exports = {
  mode: 'development',
  devtool: 'cheap-module-source-map',
  entry: './src/index.js',
  devServer:{
    port: 8000,
    hot: true
  },
  output: {
    path: path.resolve(__dirname, 'output'),
    filename: '[name].[hash:6].js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env'],
              ['@babel/preset-react']
            ],
            plugins: [
              // '@babel/plugin-transform-runtime',
              ['@babel/plugin-proposal-decorators', { 'legacy': true }],
            ]
          }
        }
      },
      {
        test: /\.css$/,
        // use: ['style-loader','css-loader'],
        use: [MiniCssExtractPlugin.loader,'css-loader'],
        // use: [MiniCssExtractPlugin.loader,{
        //   loader: 'css-loader',
        //   options:{
        //     modules: true
        //   }
        // }],
      },
      {
        test: /\.mobile$/,
        use: [MiniCssExtractPlugin.loader,'css-loader',{
          loader: './loaders/mobile-css-loader.js',
          options:{
            width: 750
          }
        }],
      }
    ]
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        react: {
          filename: 'react.js',
          chunks: 'all',
          test: /[\\/]node_modules[\\/]react[\\/]/
        },
        'react-dom': {
          filename: 'react-dom.js',
          chunks: 'all',
          test: /[\\/]node_modules[\\/]react-dom[\\/]/
        }
      }
    }
  },
  plugins: [
    new TemplatedPathPlugin(),
    new HtmlWebpackPlugin({
      template: './index.html'
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
      chunkFilename: '[id].css'
    })
  ]
}